/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.serverDB;


/**
 *
 * @author Александр Емельянов (mr.lex91@gmail.com)
 */
public class ServerDB {
    private FileDB dataBaseManager;

    public ServerDB(String dbPath)
    {
        dataBaseManager=new FileDB(dbPath);
    }
    public boolean LogIn(String nickName, String pas) throws NoSuchAccauntException
    {
        RecordInfo info;
        synchronized(dataBaseManager)
        {
            info=dataBaseManager.getRecordInfo(nickName);
        }
        if (info.getFinded()==1) return info.getData().equals(pas);
        else throw new NoSuchAccauntException();
    }
    public boolean CreateAcc(String nickName, String pas)
    {
        synchronized(dataBaseManager)
        {
            RecordInfo info=dataBaseManager.getRecordInfo(nickName);
            if ((info.getFinded()==0)) return dataBaseManager.Add(nickName, pas,0,false);
            else if ((info.getFinded()==2)) return dataBaseManager.Add(nickName, pas,info.getDiskriptor(),true);
            else return false;
        }
    }
    public boolean DeleteAcc(String nickName, String pas) throws NoSuchAccauntException
    {
        synchronized(dataBaseManager)
        {
            RecordInfo info=dataBaseManager.getRecordInfo(nickName);
            if (info.getFinded()==1) if (info.getData().equals(pas)) return dataBaseManager.Remove(info.getDiskriptor());
            else throw new NoSuchAccauntException();
            return false;
        }
    }
}
